package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import com.sky.vo.EmployeeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String password = employeeLoginDTO.getPassword();
        String username = employeeLoginDTO.getUsername();
        if (username == null || password == null) {
            throw new RuntimeException("用户名或密码为空");
        }
        Employee employee = employeeMapper.getByUsername(username);
        if (employee == null) {
            throw new RuntimeException("用户名不存在");
        }
        Integer status = employee.getStatus();
        if (status == 0) {
            throw new RuntimeException("账号被锁定");
        }
         password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!password.equals(employee.getPassword()))
        {
            throw new RuntimeException("密码错误");
        }


        return employee;


    }

    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
        Page< Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
        long total = page.getTotal();
        List<Employee> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        Long currentId = BaseContext.getCurrentId();
        employee.setCreateUser(currentId);
        employee.setUpdateUser(currentId);
        employee.setStatus(StatusConstant.ENABLE);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateTime(LocalDateTime.now());
        employeeMapper.save(employee);

    }

    @Override
    public Employee getInfo(Long id) {
        Employee info = employeeMapper.getInfo(id);
        return info;
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employeeMapper.update(employee);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
         Employee employee=Employee.builder()
                 .status(status)
                 .id(id)
                 .build();
         employeeMapper.update(employee);
    }


    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */


}
